/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: soubao-java 2020-07-22 $
 */<template>
  <div>
    <el-cascader
      ref="myCascader"
      :options="options"
      @change="sendInfo"
      :props="props"
      :placeholder="oldAddress"
    ></el-cascader>
  </div>
</template>

<script>
import request from "@/utils/request.js";
export default {
  data() {
    return {
      data: [],
      options: [],
      id: 0,
      props: {
        test: null,
        value: "id",
        label: "name",
        lazy: true,
        Obj: this,
        lazyLoad(node, resolve) {
          var that = this;
          var nodes = [];
          if (node.value != null) {
            request
              .get("/mall/region/list?parent_id=" + node.value)
              .then(function(res) {
                res.forEach(function(item, index) {
                  nodes[index] = item;
                  nodes[index].leaf = item.level >= that.Obj.level;
                });
                resolve(nodes);
              });
          }
        }
      },
      oldAddress: ""
    };
  },
  created() {
    this.getList();
    this.oldAddress = this.initVal.join(" / ");
  },
  props: ["dataName", "initVal", "level"],
  methods: {
    sendInfo(val) {
      var that = this;
      var labelArr = that.$refs["myCascader"].getCheckedNodes()[0].pathLabels;
      var opObj = that.options;
      this.$emit("choose", { value: val, key: that.dataName, label: labelArr });
    },
    getList() {
      var that = this;
      this.request
        .get("/mall/region/list?parent_id=" + that.id)
        .then(function(res) {
          that.options = res;
          that.options.forEach(function(item, index) {
            item.children = [];
          });
        });
    }
  }
};
</script>

<style scoped>
</style>
